package org.rcgwt.client.ui.grid;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.rcgwt.client.ui.grid.model.IDataModel;
import org.rcgwt.client.ui.grid.model.IFiltredDataModel;
import org.rcgwt.client.ui.grid.model.IGroupDataModel;
import org.rcgwt.client.ui.grid.provider.IAdditionalInformationProvider;
import org.rcgwt.client.ui.grid.provider.ICheckProvider;
import org.rcgwt.client.ui.grid.provider.IColumnProvider;
import org.rcgwt.client.ui.grid.provider.IFocusProvider;
import org.rcgwt.client.ui.grid.provider.IOrderProvider;
import org.rcgwt.client.ui.grid.provider.IOverProvider;
import org.rcgwt.client.ui.grid.provider.ISelectionProvider;
import org.rcgwt.client.ui.grid.theme.DefaultGridTheme;
import org.rcgwt.client.ui.grid.theme.IGridTheme;

/**
 * Base config for {@link RcGrid}
 * 
 * @author Matthieu ROBERT & Olivier Oeuillot
 * 
 */
public class RcGridConfig {

	// Model
	private IDataModel dataModel = null;

	private IFiltredDataModel filtredDataModel = null;

	private IGroupDataModel groupDataModel = null;

	private boolean editable = false;

	private boolean selectable = true;

	private boolean checkable = false;

	private boolean additionalInformations = false;

	// Default Renderer
	private IGridTheme theme = null;

	private int width = 500;

	private int height = 300;

	private int firstRowIndex = 0;

	private int pageSize = -1;

	private IColumnProvider columnProvider = null;

	private ISelectionProvider selectionProvider;

	private ICheckProvider checkProvider;

	private IAdditionalInformationProvider additionalInformationProvider;

	private IOverProvider overProvider;

	private IFocusProvider focusProvider;

	private IOrderProvider columnsOrderProvider;

	private boolean focusAutoSelect = true;

	private List<ColumnConfig> columnsConfig;

	public IDataModel getDataModel() {
		return dataModel;
	}

	public void setDataModel(IDataModel dataModel) {
		this.dataModel = dataModel;
	}

	public IFiltredDataModel getFiltredDataModel() {
		return filtredDataModel;
	}

	public void setFiltredDataModel(IFiltredDataModel filtredDataModel) {
		this.filtredDataModel = filtredDataModel;
	}

	public IGroupDataModel getGroupDataModel() {
		return groupDataModel;
	}

	public void setGroupDataModel(IGroupDataModel groupDataModel) {
		this.groupDataModel = groupDataModel;
	}

	public boolean isEditable() {
		return editable;
	}

	public void setEditable(boolean editable) {
		this.editable = editable;
	}

	public boolean isSelectable() {
		return selectable;
	}

	public void setSelectable(boolean selectable) {
		this.selectable = selectable;
	}

	public boolean isCheckable() {
		return checkable;
	}

	public void setCheckable(boolean checkable) {
		this.checkable = checkable;
	}

	public boolean isAdditionalInformations() {
		return additionalInformations;
	}

	public void setAdditionalInformations(boolean additionalInformations) {
		this.additionalInformations = additionalInformations;
	}

	public IGridTheme getTheme() {
		if (theme == null) {
			theme = DefaultGridTheme.getTheme();
		}
		return theme;
	}

	public void setTheme(IGridTheme theme) {
		this.theme = theme;
	}

	public int getFirstRowIndex() {
		return firstRowIndex;
	}

	public void setFirstRowIndex(int firstRowIndex) {
		this.firstRowIndex = firstRowIndex;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setColumnProvider(IColumnProvider columnProvider) {
		this.columnProvider = columnProvider;
	}

	public IColumnProvider getColumnProvider() {

		return columnProvider;
	}

	public int getWidth() {
		return this.width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getHeight() {
		return height;
	}

	public ISelectionProvider getSelectionProvider() {
		return selectionProvider;
	}

	public void setSelectionProvider(ISelectionProvider selectionProvider) {
		this.selectionProvider = selectionProvider;
	}

	public ICheckProvider getCheckProvider() {
		return checkProvider;
	}

	public void setCheckProvider(ICheckProvider checkProvider) {
		this.checkProvider = checkProvider;
	}

	public IAdditionalInformationProvider getAdditionalInformationProvider() {
		return additionalInformationProvider;
	}

	public void setAdditionalInformationProvider(
			IAdditionalInformationProvider additionalInformationProvider) {
		this.additionalInformationProvider = additionalInformationProvider;
	}

	public IOverProvider getOverProvider() {
		return overProvider;
	}

	public void setOverProvider(IOverProvider overProvider) {
		this.overProvider = overProvider;
	}

	public IFocusProvider getFocusProvider() {
		return focusProvider;
	}

	public void setFocusProvider(IFocusProvider focusProvider) {
		this.focusProvider = focusProvider;
	}

	public boolean isFocusAutoSelect() {
		return focusAutoSelect;
	}

	public void setFocusAutoSelect(boolean focusAutoSelect) {
		this.focusAutoSelect = focusAutoSelect;
	}

	public IOrderProvider getColumnsOrderProvider() {
		return columnsOrderProvider;
	}

	public void setColumnsOrderProvider(IOrderProvider columnsOrderProvider) {
		this.columnsOrderProvider = columnsOrderProvider;
	}

	public void addColumn(String name, String title) {
		addColumn(name, title, -1);
	}

	public void addColumn(String name, String title, int width) {
		addColumn(name, title, width, null);
	}

	public void addColumn(String name, String title, int width,
			Map<String, Object> properties) {
		if (columnsConfig == null) {
			columnsConfig = new ArrayList<ColumnConfig>();
		}

		if (properties == null) {
			properties = Collections.emptyMap();
		}

		columnsConfig.add(new ColumnConfig(name, title, width, properties));
	}

	IColumnConfig[] listColumnsConfig() {
		if (columnsConfig == null) {
			return null;
		}

		return (IColumnConfig[]) columnsConfig
				.toArray(new IColumnConfig[columnsConfig.size()]);
	}

	public interface IColumnConfig {
		String getName();

		String getTitle();

		int getWidth();

		Map<String, Object> getProperties();
	}

	private static class ColumnConfig implements IColumnConfig {
		private String name;
		private String title;
		private int width;
		private Map<String, Object> properties;

		public ColumnConfig(String name, String title, int width,
				Map<String, Object> properties) {
			this.name = name;
			this.title = title;
			this.width = width;
			this.properties = properties;
		}

		public String getName() {
			return name;
		}

		public String getTitle() {
			return title;
		}

		public int getWidth() {
			return width;
		}

		public Map<String, Object> getProperties() {
			return properties;
		}
	}
}
